package com.easylinkin.linkappapi.quality.controller;

import com.easylinkin.linkappapi.quality.dto.ProblemDTO;
import com.easylinkin.linkappapi.quality.entity.Problem;
import com.easylinkin.linkappapi.quality.service.ProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.List;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 问题库管理
 */
@RestController
@RequestMapping("problem")
@Api(tags = "问题库管理")
public class ProblemController {
  
  @Autowired
  private ProblemService problemService;

  /**
   * @Description: 增加问题库
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加问题库")
  public RestMessage insert(@RequestBody Problem problem){
    problemService.insert(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @author Tong Jie
   */
  @PostMapping("/openapi/addFromEnterprise")
  @ApiOperation("增加问题")
  public RestMessage addFromEnterprise(@RequestBody Problem problem) {
    problemService.addFromEnterprise(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @author Tong Jie
   */
  @PostMapping("/openapi/updateFromEnterprise")
  @ApiOperation("修改问题")
  public RestMessage updateFromEnterprise(@RequestBody Problem problem) {
    problemService.updateFromEnterprise(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @author Tong Jie
   */
  @PostMapping("/openapi/deleteFromEnterprise")
  @ApiOperation("删除问题")
  public RestMessage deleteFromEnterprise(@RequestBody Problem problem) {
    problemService.deleteFromEnterprise(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @author Tong Jie
   */
  @PostMapping("/openapi/listFromEnterprise")
  @ApiOperation("删除问题")
  public List<Problem> listFromEnterprise(@RequestBody Problem problem) {
    List<Problem> record = problemService.listFromEnterprise(problem);
    return record;
  }


  /**
   * @Description: 删除问题库（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除问题库（包含批量删除）")
  public RestMessage delBatch(@RequestBody Problem problem){
    Assert.notEmpty(problem.getIds(),"id不能为空");
    problemService.removeByIds(problem.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 启用/禁用
   */
  @PostMapping("/openapi/changeStatusFromEnterprise")
  @ApiOperation("启用/禁用")
  public RestMessage changeStatusFromEnterprise(@RequestBody Problem problem) {
    Assert.notEmpty(problem.getIds(), "id不能为空");
    problemService.changeStatusFromEnterprise(problem);
    return RestBuilders.successBuilder().build();
  }


  @PostMapping("synchronizeData")
  @ApiOperation("同步")
  public RestMessage synchronizeData(@RequestParam("add") Integer add, @RequestParam("cover") Integer cover){
    problemService.synchronizeData(add,cover);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 启用/禁用
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping("status")
  @ApiOperation("启用/禁用")
  public RestMessage changeStatus(@RequestBody Problem problem){
    Assert.notEmpty(problem.getIds(),"id不能为空");
    problemService.changeStatus(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 排序移动
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping("order")
  @ApiOperation("排序移动")
  public RestMessage changeOrder(@RequestBody ProblemDTO problemDTO){
    Assert.notNull(problemDTO.getId(),"id不能为空");
    problemService.changeOrder(problemDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑问题库
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑问题库")
  public RestMessage updateById(@RequestBody Problem problem){
    Assert.notNull(problem.getId(),"id不能为空");
    problemService.updateOne(problem);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询问题库详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询问题库详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    Problem problem = problemService.getById(id);
    return RestBuilders.successBuilder().data(problem).build();
  }

  /**
   * @Description: 根据条件，查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，查询")
  public RestMessage queryList(@RequestBody Problem problem){
    List<Problem> record =  problemService.queryList(problem);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 验证问题是否被引用
   */
  @GetMapping("check/{id}")
  @ApiOperation("验证问题是否被引用")
  public RestMessage check(@PathVariable("id") Integer id){
    Boolean aBoolean = problemService.check(id);
    return RestBuilders.successBuilder().data(aBoolean).build();
  }
}

